package com.zxc.shop.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxc.shop.common.constants.RabbitMQCons;
import com.zxc.shop.common.utils.PageUtils;
import com.zxc.shop.common.utils.Query;
import com.zxc.shop.common.vo.GoodsPngVo;
import com.zxc.shop.common.vo.GoodsVo;
import com.zxc.shop.goods.dao.GoodsDao;
import com.zxc.shop.goods.entity.GoodsEntity;
import com.zxc.shop.goods.entity.StockEntity;
import com.zxc.shop.goods.service.GoodsPngService;
import com.zxc.shop.goods.service.GoodsService;
import com.zxc.shop.goods.service.StockService;
import com.zxc.shop.goods.utils.VoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


@Service("goodsService")
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    private GoodsPngService goodsPngService;

    @Autowired
    private StockService stockService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {


        // 1.创建分页对象
        IPage<GoodsEntity> page1 = new Query<GoodsEntity>().getPage(params);

        // 2.创建查询对象
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();
        Object gname = params.get("gname");
        if (gname != null && !StringUtils.isEmpty(gname.toString())) {
            queryWrapper.like("gname", gname);
        }

        // 3.组合条件查询
        IPage<GoodsEntity> page = this.page(page1, queryWrapper);

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void addGoods(GoodsVo goodsVo) {

        // 1.把VO转成entity然后再保存
        GoodsEntity goodsEntity = new GoodsEntity();
        // 属性名称一样就会拷贝过去，把goods中的属性拷贝到goodsEntity中
        BeanUtils.copyProperties(goodsVo,goodsEntity);

        // 2.插入商品信息    // MP会自动完成主键回填，回填对象的id
        baseMapper.insert(goodsEntity);

        // 3.插入商品图片
        List<GoodsPngVo> goodsPngList = goodsVo.getGoodsPngList();
        for (GoodsPngVo goodsPngVo : goodsPngList) {
            goodsPngVo.setGid(goodsEntity.getId()); // 给商品的图片设置商品的id
        }
        goodsPngService.saveBatch(VoUtils.GoodsPngVOToGoodsPngEntity(goodsPngList));


        // 4.插入商品的库存
        StockEntity stockEntity = new StockEntity();
        stockEntity.setGid(goodsEntity.getId());
        stockEntity.setStock(goodsVo.getStock());
        stockEntity.setWid(1);
        stockService.save(stockEntity);


        // 把商品发送给MQ，同步给ES
        goodsVo.setId(goodsEntity.getId());
        String goodsVoJson = JSON.toJSONString(goodsVo);

        log.debug("goodsVoJson:{}",goodsVoJson);

        rabbitTemplate.convertAndSend(RabbitMQCons.GOODS_EXCHANGE,"goods.save",goodsVoJson);
    }

}